﻿

Namespace YahooManaged.Finance.Screener.API



    Public Class BondScreenerInfoDownload
        Inherits Base.Download

        Public Event AsyncDownloadCompleted(ByVal sender As Base.Download, ByVal e As BondScreenerInfoDownloadCompletedEventArgs)

        Public Function Download(ByVal res As BondScreenerData) As BondScreenerInfoResponse
            Return Me.ToResponse(MyBase.DownloadStream(Me.DownloadUrl(res)), res)
        End Function

        Public Sub DownloadAsync(ByVal res As BondScreenerData, Optional ByVal userArgs As Object = Nothing)
            Dim args As New AsyncArgs(userArgs, res)
            MyBase.DownloadStreamAsync(Me.DownloadUrl(args.Result), args)
        End Sub

        Private Sub AsyncDownload_Completed(ByVal sender As Base.Download, ByVal e As Base.StreamDownloadCompletedEventArgs) Handles MyBase.AsyncStreamDownloadCompleted
            Dim args = CType(e.UserArgs, AsyncArgs)
            RaiseEvent AsyncDownloadCompleted(Me, New BondScreenerInfoDownloadCompletedEventArgs(args.UserArgs, Me.ToResponse(e.Response, args.Result)))
        End Sub

        Private Function DownloadUrl(ByVal res As BondScreenerData) As String
            Dim whereClause As New Text.StringBuilder
            whereClause.Append("url=""")
            whereClause.Append(res.Issue.Url.ToString)
            whereClause.Append(""" AND xpath='/html/body/table/tr/td/table[3]/tr/td[1]'")
            Return mHelper.YqlUrl("*", "html", whereClause.ToString, Nothing, False)
        End Function

        Private Function ToResponse(ByVal resp As Base.StreamResponse, ByVal oldData As BondScreenerData) As BondScreenerInfoResponse
            Dim res As New BondScreenerInfo
            Dim convCulture As New Globalization.CultureInfo("en-US")
            Dim resultsNodes = mHelper.GetResultsFromXml(resp.Result, "results")
            If resultsNodes.Length > 0 Then
                Dim resultNode = resultsNodes(0)
                If resultNode.ChildNodes.Count > 0 Then
                    Dim tdNode = resultNode.FirstChild
                    Dim tableIndex As Integer = 0
                    For Each tableNode As XmlNode In tdNode.ChildNodes
                        Select Case tableIndex
                            Case 0
                                Dim trNode = tableNode.ChildNodes(1)
                                res.Title = trNode.FirstChild.FirstChild.FirstChild.InnerText
                                Date.TryParseExact(trNode.LastChild.FirstChild.InnerText.Replace("As of ", ""), "dd-MMM-yyyy", convCulture, Globalization.DateTimeStyles.None, res.AsOf)

                            Case 3
                                Dim innerTableNode = tableNode.FirstChild.FirstChild.FirstChild
                                Dim innerIndex As Integer = 0
                                For Each innerTrNode As XmlNode In innerTableNode.ChildNodes
                                    Select Case innerIndex
                                        Case 0 : Double.TryParse(innerTrNode.LastChild.InnerText, Globalization.NumberStyles.Any, convCulture, res.Price)
                                        Case 1 : Double.TryParse(innerTrNode.LastChild.InnerText, Globalization.NumberStyles.Any, convCulture, res.CouponInPercent)
                                        Case 2 : Date.TryParseExact(innerTrNode.LastChild.InnerText, "dd-MMM-yyyy", convCulture, Globalization.DateTimeStyles.None, res.Maturity)
                                        Case 3 : Double.TryParse(innerTrNode.LastChild.InnerText, Globalization.NumberStyles.Any, convCulture, res.YieldToMaturityInPercent)
                                        Case 4 : Double.TryParse(innerTrNode.LastChild.InnerText, Globalization.NumberStyles.Any, convCulture, res.CurrentYieldInPercent)
                                        Case 5 : res.CouponPaymentFrequency = innerTrNode.LastChild.InnerText
                                        Case 6 : Date.TryParseExact(innerTrNode.LastChild.InnerText, "dd-MMM-yyyy", convCulture, Globalization.DateTimeStyles.None, res.FirstCouponDate)
                                        Case 7
                                            Select Case innerTrNode.LastChild.InnerText
                                                Case "Treasury" : res.Type = BondType.Treasury
                                                Case "Treasury Zero" : res.Type = BondType.TreasuryZeroCoupon
                                                Case "Corporate" : res.Type = BondType.Corporate
                                                Case "Municipal" : res.Type = BondType.Municipal
                                            End Select
                                        Case 8 : res.Callable = CBool(IIf(innerTrNode.LastChild.InnerText = "Yes", True, False))
                                    End Select
                                    innerIndex += 1
                                Next

                            Case 7
                                Dim innerTableNode = tableNode.FirstChild.FirstChild.FirstChild
                                Dim innerIndex As Integer = 0
                                For Each innerTrNode As XmlNode In innerTableNode.ChildNodes
                                    Select Case innerIndex
                                        Case 0 : Integer.TryParse(innerTrNode.LastChild.InnerText, Globalization.NumberStyles.Any, convCulture, res.AvailableQuantity)
                                        Case 1 : Integer.TryParse(innerTrNode.LastChild.InnerText, Globalization.NumberStyles.Any, convCulture, res.MinimumTradeQuantity)
                                        Case 2 : Date.TryParseExact(innerTrNode.LastChild.InnerText, "dd-MMM-yyyy", convCulture, Globalization.DateTimeStyles.None, res.DatedDate)
                                        Case 3 : Date.TryParseExact(innerTrNode.LastChild.InnerText, "dd-MMM-yyyy", convCulture, Globalization.DateTimeStyles.None, res.SettlementDate)
                                    End Select
                                    innerIndex += 1
                                Next
                        End Select
                        tableIndex += 1
                    Next
                    res.Issue = New Base.Link(oldData.Issue.Title, oldData.Issue.Url)
                    res.FitchRating = oldData.FitchRating
                End If
            End If
            Return New BondScreenerInfoResponse(resp.Connection, res)
        End Function

        Private Class AsyncArgs
            Inherits Base.DownloadEventArgs
            Public Result As BondScreenerData
            Public Sub New(ByVal userArgs As Object, ByVal res As BondScreenerData)
                MyBase.New(userArgs)
                Me.Result = res
            End Sub
        End Class

    End Class


    Public Class BondScreenerInfoResponse
        Inherits Base.Response

        Public Overloads ReadOnly Property Result As BondScreenerInfo
            Get
                Return CType(MyBase.Result, BondScreenerInfo)
            End Get
        End Property

        Friend Sub New(ByVal conn As Base.ConnectionInfo, ByVal result As BondScreenerInfo)
            MyBase.New(conn, result)
        End Sub

    End Class



    Public Class BondScreenerInfoDownloadCompletedEventArgs
        Inherits Base.DownloadCompletedEventArgs

        Public Overloads ReadOnly Property Response As BondScreenerInfoResponse
            Get
                Return CType(MyBase.Response, BondScreenerInfoResponse)
            End Get
        End Property

        Friend Sub New(ByVal userArgs As Object, ByVal resp As BondScreenerInfoResponse)
            MyBase.New(userArgs, resp)
        End Sub

    End Class


End Namespace